//-----------------------------------------------------------------------
// <copyright file="VirtualServerTaskBase.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Mark Phillips</author>
// <email>v-mphill@microsoft.com</email>
// <date>2004-05-24</date>
// <summary>provides a base class for all tasks that call to a Virtual Server COM component.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.VirtualServer
{
    using System;
    using System.Reflection;
    using System.Collections;
    using System.Runtime.InteropServices;
    using Microsoft.Sdc.Tasks;

    /// <summary>
    /// InitVS handles the special COM/DCOM startup code required by the Virtual
    /// Server security model.
    /// </summary>
    public abstract class VirtualServerBase : TaskBase
    {
        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
        }

        internal ComWrapper GetVirtualServer(string server)
        {
            ComWrapper virtualServer = new ComWrapper("VirtualServer.Application", server, true);
            return virtualServer;
        }

        internal ComWrapper GetVirtualMachinesFromServer(string server)
        {
            ComWrapper virtualServer = GetVirtualServer(server);
            ComWrapper virtualMachines = new ComWrapper(virtualServer.GetProperty("VirtualMachines"), true);

            int count = (int)virtualMachines.GetProperty("Count");

            if (count == 0)
            {
                throw new TaskException("VirtualServer.NoMachinesConfigured", null);
            }
            return virtualMachines;
        }

        internal ComWrapper GetVirtualMachine(ComWrapper virtualMachines, string machineName)
        {
            int count = (int)virtualMachines.GetProperty("Count");

            if (count == 0)
            {
                throw new TaskException("VirtualServer.NoMachinesConfigured", null);
            }

            machineName = machineName.ToLowerInvariant();

            for (int i = 0; i < count; i++)
            {
                ComWrapper virtualMachine = new ComWrapper(virtualMachines.GetProperty("Item", i + 1), true);

                string indexMachineName = ((string)virtualMachine.GetProperty("Name")).ToLowerInvariant();

                if (machineName == indexMachineName)
                {
                    return virtualMachine;
                }
            }
            throw new TaskException("VirtualServer.FailedToLocateMachine", machineName);
        }
    }
}